package com.dhh.toodledo.util;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dhh.toodledo.model.AccountInfo;
import com.dhh.toodledo.model.Task;
import com.dhh.toodledo.model.Tasks;

public class JsonUtil {
	public static AccountInfo parseAccount(String json)throws Exception{
		AccountInfo  account = null;

		JSONObject obj = new JSONObject(json);
		String userid = obj.getString("userid");
		String alias = obj.getString("alias");
		int pro = obj.getInt("pro");
		int dateformat = obj.getInt("dateformat");
		int timezone = obj.getInt("timezone");
		int hidemonths = obj.getInt("hidemonths");
		int hotlistpriority = obj.getInt("hotlistpriority");
		int hotlistduedate = obj.getInt("hotlistduedate");
		int hotliststar = obj.getInt("hotliststar");
		int hotliststatus = obj.getInt("hotliststatus");
		int showtabnums = obj.getInt("showtabnums");
		int lastedit_task = obj.getInt("lastedit_task");
		int lastdelete_task = obj.getInt("lastdelete_task");
		int lastedit_folder = obj.getInt("lastedit_folder");
		int lastedit_context = obj.getInt("lastedit_context");
		int lastedit_goal = obj.getInt("lastedit_goal");
		int lastedit_location = obj.getInt("lastedit_location");
		int lastedit_notebook = obj.getInt("lastedit_notebook");
		int lastdelete_notebook = obj.getInt("lastdelete_notebook");			
		
		account = new AccountInfo();
		account.setUserid(userid);
		account.setAlias(alias);
		account.setPro(pro);
		account.setDateformat(dateformat);
		account.setTimezone(timezone);
		account.setHidemonths(hidemonths);
		account.setHotlistduedate(hotlistduedate);
		account.setHotlistpriority(hotlistpriority);
		account.setHotliststar(hotliststar);
		account.setHotliststatus(hotliststatus);
		account.setShowtabnums(showtabnums);
		account.setLastedit_context(lastedit_context);
		account.setLastedit_task(lastedit_task);
		account.setLastdelete_task(lastdelete_task);
		account.setLastedit_folder(lastedit_folder);
		account.setLastedit_goal(lastedit_goal);
		account.setLastedit_location(lastedit_location);
		account.setLastedit_notebook(lastedit_notebook);
		account.setLastdelete_notebook(lastdelete_notebook);			
		
	
		
		return account;
	}
	
	public static boolean isError(String json){
		boolean error = false;
		try{
			JSONObject obj = new JSONObject(json);			
			if (!obj.isNull("errorCode"))
				error = true;
		}catch(Exception e){}
		
		return error;
		
	}
	
	public static Task parseTask(String json)throws Exception{
		Task t = new Task();

		JSONArray array = new JSONArray(json);
		JSONObject to = array.getJSONObject(0);
		if (!to.isNull("id"))
			t.setId(to.getLong("id"));
		if (!to.isNull("title"))
			t.setTitle(to.getString("title"));
		if (!to.isNull("tag"))
			t.setTag(to.getString("tag"));
		if (!to.isNull("folder"))
			t.setFolder(to.getLong("folder"));
		if (!to.isNull("context"))
			t.setContext(to.getLong("context"));
		if (!to.isNull("goal"))
			t.setGoal(to.getLong("goal"));
		if (!to.isNull("location"))
			t.setLocation(to.getLong("location"));
		if (!to.isNull("parent"))
			t.setParent(to.getLong("parent"));
		if (!to.isNull("children"))
			t.setChildren(to.getLong("children"));
		if (!to.isNull("order"))
			t.setOrder(to.getLong("order"));
		if (!to.isNull("duedate"))
			t.setDuedate(to.getLong("duedate"));
		if (!to.isNull("duedatemod"))
			t.setDuedatemod(to.getLong("duedatemod"));
		if (!to.isNull("startdate"))
			t.setStartdate(to.getLong("startdate"));
		if (!to.isNull("duetime"))
			t.setDuetime(to.getLong("duetime"));
		if (!to.isNull("starttime"))
			t.setStarttime(to.getLong("starttime"));
		if (!to.isNull("remind"))
			t.setRemind(to.getLong("remind"));
		if (!to.isNull("repeat"))
			t.setRepeat(to.getLong("repeat"));
		if (!to.isNull("repeatfrom"))
			t.setRepeatfrom(to.getLong("repeatfrom"));
		if (!to.isNull("status"))
			t.setStatus(to.getLong("status"));
		if (!to.isNull("length"))
			t.setLength(to.getLong("length"));
		if (!to.isNull("priority"))
			t.setPriority(to.getLong("priority"));
		if (!to.isNull("star"))
			t.setStar(to.getLong("star"));
		if (!to.isNull("modified"))
			t.setModified(to.getLong("modified"));
		if (!to.isNull("completed"))
			t.setCompleted(to.getLong("completed"));
		if (!to.isNull("added"))
			t.setAdded(to.getLong("added"));
		if (!to.isNull("timer"))
			t.setTimer(to.getLong("timer"));
		if (!to.isNull("timeron"))
			t.setTimeron(to.getLong("timeron"));
		if (!to.isNull("note"))
			t.setNote(to.getString("note"));
		if (!to.isNull("meta"))
			t.setMeta(to.getString("meta"));
	
		return t;
	}
	
	public static Tasks parseTasks(String json)throws Exception{
		Tasks tasks = new Tasks();

		JSONArray array = new JSONArray(json);
		JSONObject sm = array.getJSONObject(0);
		tasks.setNum(sm.getInt("num"));
		tasks.setTotal(sm.getInt("total"));
		
		for(int i=1; i<array.length(); i++){
			JSONObject to = array.getJSONObject(i);
			Task t = new Task();
			if (!to.isNull("id"))
				t.setId(to.getLong("id"));
			if (!to.isNull("title"))
				t.setTitle(to.getString("title"));
			if (!to.isNull("tag"))
				t.setTag(to.getString("tag"));
			if (!to.isNull("folder"))
				t.setFolder(to.getLong("folder"));
			if (!to.isNull("context"))
				t.setContext(to.getLong("context"));
			if (!to.isNull("goal"))
				t.setGoal(to.getLong("goal"));
			if (!to.isNull("location"))
				t.setLocation(to.getLong("location"));
			if (!to.isNull("parent"))
				t.setParent(to.getLong("parent"));
			if (!to.isNull("children"))
				t.setChildren(to.getLong("children"));
			if (!to.isNull("order"))
				t.setOrder(to.getLong("order"));
			if (!to.isNull("duedate"))
				t.setDuedate(to.getLong("duedate"));
			if (!to.isNull("duedatemod"))
				t.setDuedatemod(to.getLong("duedatemod"));
			if (!to.isNull("startdate"))
				t.setStartdate(to.getLong("startdate"));
			if (!to.isNull("duetime"))
				t.setDuetime(to.getLong("duetime"));
			if (!to.isNull("starttime"))
				t.setStarttime(to.getLong("starttime"));
			if (!to.isNull("remind"))
				t.setRemind(to.getLong("remind"));
			if (!to.isNull("repeat"))
				t.setRepeat(to.getLong("repeat"));
			if (!to.isNull("repeatfrom"))
				t.setRepeatfrom(to.getLong("repeatfrom"));
			if (!to.isNull("status"))
				t.setStatus(to.getLong("status"));
			if (!to.isNull("length"))
				t.setLength(to.getLong("length"));
			if (!to.isNull("priority"))
				t.setPriority(to.getLong("priority"));
			if (!to.isNull("star"))
				t.setStar(to.getLong("star"));
			if (!to.isNull("modified"))
				t.setModified(to.getLong("modified"));
			if (!to.isNull("completed"))
				t.setCompleted(to.getLong("completed"));
			if (!to.isNull("added"))
				t.setAdded(to.getLong("added"));
			if (!to.isNull("timer"))
				t.setTimer(to.getLong("timer"));
			if (!to.isNull("timeron"))
				t.setTimeron(to.getLong("timeron"));
			if (!to.isNull("note"))
				t.setNote(to.getString("note"));
			if (!to.isNull("meta"))
				t.setMeta(to.getString("meta"));
			tasks.getTasks().add(t);
		}
		
		
		return tasks;
	}
	
	
	
}
